package com.runshu.newsmartark.mvp.presenter;

import android.os.AsyncTask;

import com.google.gson.Gson;
import com.runshu.newsmartark.dbutil.DBUtils;
import com.runshu.newsmartark.entity.NewTakeStockEntity;
import com.runshu.newsmartark.entity.ProductsBean;
import com.runshu.newsmartark.mvp.contract.ProductsByCategoryContract;
import com.runshu.newsmartark.network.netbean.ResponseBean;
import com.runshu.newsmartark.network.netinterface.MyCallBack;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Demo class
 *
 * @author yu
 * @date 2020/11/3
 * @decs：
 */

public class ProductsByCategoryPresenter implements ProductsByCategoryContract.Presenter {
    private ProductsByCategoryContract.View view;
    private ProductsByCategoryContract.Model model;

    public ProductsByCategoryPresenter(ProductsByCategoryContract.View view, ProductsByCategoryContract.Model model) {
        this.view = view;
        this.model = model;
    }

    @Override
    public void getProDta(String macAddress, String category, String stockName, String modle, String manufacturer, int expiryDate, int type, int pageSize, int pageNum) {
        // 0 走接口 全部柜子数据  1走本地数据库
        if (type == 0) {
            model.getProDta( macAddress, category, stockName, modle, manufacturer, expiryDate, type, pageSize, pageNum, new MyCallBack() {
                @Override
                public void onSuccess(ResponseBean data) {
                    view.onGetProDtaSuccess( data );
                }

                @Override
                public void onError(ResponseBean data) {
                    view.onError( data.getMessage() );
                }

                @Override
                public void onErrorForOthers(ResponseBean msg) {
                    view.onError( msg.getMessage() );
                }
            } );
        } else {
            ProDBTask cg = new ProDBTask( view );
            Map<String, String> params = new HashMap<>();
            params.put( "category", category );
            params.put( "modle", modle );
            params.put( "manufacturer", manufacturer );
            cg.execute( params );
        }


    }

    @Override
    public void getInventoryList(String customerId, String hospitalId, String hwId, int pageSize, int pageNum) {
        model.getInventoryList(customerId, hospitalId, hwId, pageSize, pageNum, new MyCallBack() {
            @Override
            public void onSuccess(ResponseBean data) {
                view.onGetInventoryListSuccess(data);
            }

            @Override
            public void onError(ResponseBean data) {
                view.onError(data.getMessage());
            }
        });
    }

    //获取本柜所有商品列表
    static class ProDBTask extends AsyncTask<Map<String, String>, Integer, List<ProductsBean>> {
        private ProductsByCategoryContract.View view;

        public ProDBTask(ProductsByCategoryContract.View view) {
            this.view = view;
        }

        @Override
        protected List<ProductsBean> doInBackground(Map<String, String>... params) {
            String category = params[0].get( "category" );
            String modle = params[0].get( "modle" );
            String manufacturer = params[0].get( "manufacturer" );
            return DBUtils.getInstance().getproByType( category, modle, manufacturer );
        }

        @Override
        protected void onPostExecute(List<ProductsBean> productsBeans) {
            ResponseBean responseBean = new ResponseBean();
            NewTakeStockEntity stockEntity = new NewTakeStockEntity();
            int allStock = 0;
            if (productsBeans != null && productsBeans.size() != 0) {
                for (ProductsBean tmp : productsBeans) {
                    allStock += tmp.getStock();
                }
            }
            stockEntity.setStock( String.valueOf( allStock ) );
            stockEntity.setList( productsBeans );
            responseBean.setData( new Gson().toJson( stockEntity ) );
            responseBean.setCode( "200" );
            responseBean.setMessage( "cg" );
            view.onGetProDtaSuccess( responseBean );
        }
    }
}